class Ejecutivo < ActiveRecord::Base
  has_many :centros, :conditions => {:status_id => 1}, :order => 'nombre_comercial'
  has_many :corporativos, :conditions => {:status_id => 1}
  has_many :convenios, :conditions => {:status_id => 1}
  has_many :presupuestos, :conditions => {:status_id => 1}
  has_many :concursos, :conditions => {:status_id => 1}
  has_many :evaluaciones, :conditions => {:status_id => 1}
  has_many :promociones, :conditions => {:status_id => 1}
  has_many :presencias, :conditions => {:status_id => 1}
  has_many :salida_materiales, :conditions => {:status_id => 1}

  acts_as_tree :foreign_key => :reporta_a_id
  
  validates_presence_of :nombre, :primer_apellido, :usuario, :contrasenia, :puesto_id, :on => :create
  
  validates_length_of :nombre, :primer_apellido, :segundo_apellido, :maximum => 40, :on => :create
  
  validates_format_of :correo_electronico, :with => /^([a-zA-Z0-9_.+])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/, :on => :create
  
  validates_uniqueness_of :usuario, :on => :create
  
  validates_confirmation_of :contrasenia
  
  def nombre_completo
    "#{nombre} #{primer_apellido} #{segundo_apellido}".strip unless :segundo_apellido.nil?
    
  end
  
  def self.authenticate(usuario, contrasenia)
    Ejecutivo.find(:first, :conditions => ['(usuario=?) AND (contrasenia=?) AND (status_id=1)',
        usuario,contrasenia])
    
  end
    
  def self.personal_cuervo
    conditions = {}
    conditions[:puesto_id] = [1,2,3,4,5,7,9]
    conditions[:status_id] = 1
    
    Ejecutivo.find(:all, :conditions => conditions, :order => 'nombre, primer_apellido')
  end

  def all_active_children(ejecutivo = self)
    local_children = ejecutivo.children.dup.delete_if {|e| e.status_id == 0}
    ids = Array.new

    local_children.each do |e|
      ids << self.all_active_children(e)
    end if ejecutivo.children.size > 0

    ids << ejecutivo if ejecutivo.status_id == 1
    ids.flatten
  end
  
  def active_children
    self.children.dup.delete_if {|e| e.status_id == 0}
  end
  
  def universo_corporativos
    conditions = [
      "status_id = :status_id AND ejecutivo_id IN(:these_children)",
      {:status_id => 1, :these_children => self.all_active_children}
    ]
    Corporativo.find(:all, :conditions => conditions).size
    
  end
  
  def universo_centros
    conditions = [
      "status_id = :status_id AND ejecutivo_id IN(:these_children)",
      {:status_id => 1, :these_children => self.all_active_children}
    ]
    Centro.find(:all, :conditions => conditions).size
  end
  
  def quitar_jefe
    update_attribute('reporta_a_id', 0)
    save!
  end
  
  def not_my_people
    # activos = status_id == 1
    # asignados a otro jefe
    # OR sin jefe
    # personal cuervo
    conditions = [
      "(status_id = 1) AND ((reporta_a_id <> :this_id) OR (reporta_a_id = 0)) AND (puesto_id IN (1,2,3,4,5,7,9)) AND ([id] NOT IN(:this_id))",
      {:this_id => self.id}
    ]
    
    Ejecutivo.find(:all, :conditions => conditions, :order => 'nombre,primer_apellido')
  end
  
  def gerente?
    gerente_id = Catalogo.valor_id('Puestos Ejecutivos','Gerente CDC')
    gerente_op_id = Catalogo.valor_id('Puestos Ejecutivos','Gerente OP')
    
    self.puesto_id == gerente_id || self.puesto_id == gerente_op_id
    
  end
  
  def parent_list(ejecutivo = self)
    parents = Array.new
    
    parents << ejecutivo.parent unless ejecutivo.parent.blank?
    
    if parents.size > 0
      parents << parent_list(ejecutivo.parent)
    end
        
    parents.flatten
    
  end
end
